<template>
    <el-drawer :size="dialogWidth" :title="configureTitle" v-model="configureOpen" append-to-body
               :modal="false" @close="close">
        <el-select v-model="modelId" placeholder="请选择模型"
                   @change="handleModelChange">
            <el-option
                    v-for="agreementModel in agreementModelList"
                    :key="agreementModel.modelId"
                    :label="agreementModel.modelName"
                    :value="agreementModel.modelId">
            </el-option>
        </el-select>
        <div style="margin-left: 20px">
            <el-form ref="form" :inline="true" :model="item" v-for="item in exampleValues">
                <el-form-item label="输入属性名：">
                    <el-input v-model="item.attributeName" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="输入属性类型：">
                    <el-input v-model="item.attributeType" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="输入属性值：">
                    <el-input v-model="item.attributeValue"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div align="center">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-drawer>
</template>

<script>
    import {addAgreementExampleValues} from "/src/api/property/example";
    import {getAgreementModelList, getAgreementAttributeList} from "/src/api/property/model";

    export default {
        name: "configure",
        emits: ['configureStatusChanged'],
        props: {
            //模型ID
            modelId: Number,
            //实例ID
            exampleId: Number,
            //标题
            configureTitle: String,
            //显隐
            configureOpen: Boolean,
            //属性列表
            items: Array,
        },
        data() {
            return {
                dialogWidth: '60%',
                queryParams: {},
                //模板列表
                agreementModelList: [],
                //属性列表
                exampleValues: [],
            }
        },
        computed: {
            //监测页面开关
            monitorOpenStatus() {
                return this.configureOpen
            },
        },
        watch: {
            monitorOpenStatus(newVal) {
                if (newVal) {
                    if (this.items && this.items.length > 0) {
                        this.exampleValues = this.items
                    } else {
                        this.handleModelChange(this.modelId)
                    }
                }
            },
        },
        created() {
            this.getAgreementModelListByParam()
        },
        methods: {
            //查询模型列表
            getAgreementModelListByParam() {
                getAgreementModelList(this.queryParams).then((response) => {
                    this.agreementModelList = response.data;
                })
            },
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        //保存属性信息
                        this.exampleValues.forEach((exampleValue) => {
                            exampleValue.exampleId = this.exampleId
                        })
                        addAgreementExampleValues(this.exampleValues).then(response => {
                            this.msgSuccess(this.configureTitle + "成功");
                            this.close();
                        });
                    }
                })
            },
            delModelAttribute() {
                this.items.pop()
            },
            //取消
            cancel() {
                this.$emit('configureStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('configureStatusChanged', false)
            },
            handleModelChange(modelId) {
                //根据模型ID查询
                getAgreementAttributeList(modelId).then(response => {
                    this.exampleValues = response.data
                })
            }
        },
    }
</script>

<style scoped>

</style>
